package com.dididoctor.patient.Activity.OneToOneChat.rec;

import android.media.MediaRecorder;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Utils.LogUtil;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by 张强 on 2015/5/3.
 */
public class RecordAudio {

    private MediaRecorder mediaRecorder;
    private String mDir;
    private String mCurFilePath;
    private RecordStateListener mListener;

    private boolean isPrepared;

    public String getFilePath() {
        return mCurFilePath;
    }


    public interface RecordStateListener {
        public void wellPrepared();
    }

    public void setRecordStataListener(RecordStateListener listener) {
        LogUtil.info("RecordAudio====的RecordStateListener的监听事件开始响应", ConstantValue.currentStage);
        mListener = listener;
    }


    private static RecordAudio mInstance;

    private RecordAudio() {
    }

    ;

    public static RecordAudio getInstance(String filePath) {
        if (mInstance == null) {
            synchronized (RecordAudio.class) {
                if (mInstance == null) {
                    mInstance = new RecordAudio();
                    mInstance.mDir = filePath;
                }
            }
        }
        return mInstance;
    }


    public void prepareAudio() {
        isPrepared = false;
        File dir = new File(mDir);
        if (!dir.exists()) dir.mkdirs();
        String fileName = generateFileName();
        File file = new File(dir, fileName);
        mCurFilePath = file.getAbsolutePath();
        try {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setOutputFile(mCurFilePath);//音频输出路径
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//音频源：麦克风
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);//音频格式
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频编码
            mediaRecorder.prepare();
            mediaRecorder.start();
            isPrepared = true;
            if (mListener != null) {
                LogUtil.info("RecordAudio====进入了wellPrepared方法，打开了录音机", ConstantValue.currentStage);
                mListener.wellPrepared();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String generateFileName() {
        return UUID.randomUUID().toString() + ".amr";
    }


    public int getVoiceLevel(int maxLevel) {
        if (isPrepared) {
            try {
                return maxLevel * mediaRecorder.getMaxAmplitude() / 32768 + 1;
            } catch (Exception e) {

            }
        }
        return 1;
    }

    public void release() {
        if (mediaRecorder != null || isPrepared) {
            try {
                mediaRecorder.setOnErrorListener(null);
                mediaRecorder.stop();
                mediaRecorder.release();
            } catch (IllegalStateException e) {
            } finally {
                mediaRecorder = null;
            }
        }
    }

    public void cancel() {
        release();
        if (mCurFilePath != null) {
            File file = new File(mCurFilePath);
            file.delete();
            mCurFilePath = null;
            file = null;
        }
    }


}
